package com.quan.system.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.common.core.MyBaseServiceImpl;
import com.quan.common.enums.SchedulerStatus;
import com.quan.common.handler.scheduler.SchedulerUtils;
import com.quan.system.entity.SystemJob;
import com.quan.system.mapper.SystemJobMapper;
import com.quan.system.service.SystemJobService;

/**
 * <p>
 * 系统-定时作业管理 服务实现类
 * </p>
 *
 * @author 杨浩泉
 * @since 2019-06-30
 */
@Service
public class SystemJobServiceImpl extends MyBaseServiceImpl<SystemJobMapper, SystemJob> implements SystemJobService {

	@Autowired
	private SchedulerUtils schedulerUtils;

	@Override
	public boolean startJob(SystemJob entity) {
		if (StringUtils.isBlank(entity.getJobStatus())) {
			entity.setJobStatus(SchedulerStatus.RUNNING.getName());
		}
		if (StringUtils.isBlank(entity.getJobGroup())) {
			entity.setJobGroup(Scheduler.DEFAULT_GROUP);
		}

		// 先把旧的定时任务删除
		QueryWrapper<SystemJob> queryWrapper = new QueryWrapper<SystemJob>();
		queryWrapper.eq("job_name", entity.getJobName()) ;
		List<SystemJob> list = super.list(queryWrapper);
		if(null != list && !list.isEmpty()) {
			list.forEach(item->{
				this.schedulerUtils.deleteJob(item.getJobName(), item.getJobGroup());
				super.removeById(item.getId());
			});
		}
		
		boolean flag = super.save(entity);
		if (flag) {
			// 新建定时任务
			this.schedulerUtils.newJob(entity);

			// 任务为暂停状态
			if (entity.getJobStatus().equals(SchedulerStatus.PAUSE.getName())) {
				this.pauseJob(entity.getId(), entity.getJobName(), entity.getJobGroup());
			}
		}

		return flag;
	}

	@Override
	public boolean removeByIds(Collection<? extends Serializable> idList) {
		// 先把定时任务删除
		Collection<SystemJob> list = super.listByIds(idList);
		list.forEach(item -> {
			this.schedulerUtils.deleteJob(item.getJobName(), item.getJobGroup());
		});

		return super.removeByIds(idList);
	}

	@Override
	public boolean updateById(SystemJob entity) {
		// 先把旧的定时任务删除
		SystemJob oldJob = super.getById(entity.getId());
		this.schedulerUtils.deleteJob(oldJob.getJobName(), oldJob.getJobGroup());

		boolean flag = super.updateById(entity);
		if (flag) {
			// 重新新建定时任务
			this.schedulerUtils.newJob(entity);
		}

		return super.updateById(entity);
	}

	@Override
	public void deleteJobByName(String jobName) {
		this.deleteJobByName(jobName, Scheduler.DEFAULT_GROUP);
	}

	@Override
	public void deleteJobByName(String jobName, String jobGroup) {
		QueryWrapper<SystemJob> queryWrapper = new QueryWrapper<SystemJob>();
		queryWrapper.eq("job_name", jobName).eq("job_group", jobGroup);
		List<SystemJob> list = super.list(queryWrapper) ;
		
		if(null != list && !list.isEmpty()) {
			List<Serializable> delIds = new ArrayList<Serializable>();
			list.forEach(item -> {
				delIds.add(item.getId());
				this.schedulerUtils.deleteJob(item.getJobName(), item.getJobGroup());
			});
			super.removeByIds(delIds);
		}
		
	}

	@Override
	public void pauseJob(Long id, String jobName, String jobGroup) {
		this.changeStatus(id, SchedulerStatus.PAUSE.getName());
		this.schedulerUtils.pauseJob(jobName, jobGroup);
	}

	@Override
	public void resumeJob(Long id, String jobName, String jobGroup) {
		this.changeStatus(id, SchedulerStatus.RUNNING.getName());
		this.schedulerUtils.resumeJob(jobName, jobGroup);
	}

	@Override
	public void triggerJob(Long id, String jobName, String jobGroup) {
		this.schedulerUtils.triggerJob(jobName, jobGroup);
	}

	@Override
	public void changeStatus(Long id, String jobStatus) {
		SystemJob entity = new SystemJob();
		entity.setId(id);
		entity.setJobStatus(jobStatus);
		super.updateById(entity);
	}

}
